﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using System.Collections;
using System.Text.RegularExpressions;

namespace CrazyKTVfromCashBox
{
    class Gmethods
    {

        public static string UrlEncode(string unEncodedString, string GetEncoding)
        {

            if (GetEncoding == "GB2312")
            {
                return string.Concat(Encoding
                                       .GetEncoding(GetEncoding)
                                       .GetBytes(unEncodedString)
                                       .Select(x => "%" + x.ToString("x2"))
                                       .ToArray());
            }
            else
            {
                // Sytem.Uri provides reliable parsing
                return System.Uri.EscapeDataString(unEncodedString);
           
            }

            //System.Web.HttpServerUtility.Server.UrlEncode("ASP.NET Examples");
        }



        public static string UrlEncode2(string unEncodedString)
        {
             return System.Uri.EscapeDataString(unEncodedString);
        }






        internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

        /// <summary> 
        ///使用OS的kernel.dll做為簡繁轉換工具，只要有裝OS就可以使用，不用額外引用dll，但只能做逐字轉換，無法進行詞意的轉換 
        /// <para>所以無法將電腦轉成計算機</para> 
        /// </summary> 
        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out]  string lpDestStr, int cchDest);

        /// <summary > 
        ///繁體轉簡體 
        /// </summary> 
        /// <param name="pSource">要轉換的繁體字：體</param> 
        /// <returns>轉換後的簡體字：體</ returns> 
        public static string ToSimplified(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }

        /// <summary> 
        ///簡體轉繁體 
        /// </summary> 
        /// <param name="pSource">要轉換的繁體字：體</param> 
        /// <returns>轉換後的簡體字：體</returns> 
        public static string ToTraditional(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }




        public static string replaceQueryString(string url, string key, string value)
        {
            return System.Text.RegularExpressions.Regex.Replace(
                url,
                @"([?&]" + key + ")=[^?&]+",
                "$1=" + value);
        }

        public static string getQueryString(string url, string key)
        {
           // System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"([?&]" + key + ")=[^?&]+");
            Match match = Regex.Match(url, @"([?&]" + key + ")=[^?&]+");

            if (match.Success)
            {
                // Finally, we get the Group value and display it.
                return match.Groups.SyncRoot.ToString().Replace(match.Groups[1].Value + "=","");
            }

            return ""; // find nothing

        }




    }
}
